#!/bin/bash

docker network create --driver bridge --subnet 172.18.0.0/24 --gateway 172.18.0.1 mynet

##docker pull mysql:8.0.20
docker pull mysql:5.7

export dockerPath=/opt/docker
mysqlPath=$dockerPath/mysql
mysqlDataPath=$mysqlPath/data
mysqlConfPath=$mysqlPath/conf
mysql_sPath=$dockerPath/mysql_s
mysql_sDataPath=$mysql_sPath/data
mysql_sConfPath=$mysql_sPath/conf

mysqlSourceConfFile=mysql:/etc/mysql/mysql.conf.d/mysqld.cnf
mysqlTargetConfFile=$mysqlConfPath/mysqld.cnf

mysql_sSourceConfFile=mysql_s:/etc/mysql/mysql.conf.d/mysqld.cnf
mysql_sTargetConfFile=$mysql_sConfPath/mysqld.cnf

mkdir -p $dockerPath
mkdir -p $mysqlPath
mkdir -p $mysqlDataPath
mkdir -p $mysqlConfPath

mkdir -p $mysql_sPath
mkdir -p $mysql_sDataPath
mkdir -p $mysql_sConfPath

docker run -d --name mysql --restart=always \
-p 3301:3306 \
-e MYSQL_ROOT_PASSWORD=testYnlcom \
--net mynet \
--ip 172.18.0.2 \
mysql:5.7

docker cp $mysqlSourceConfFile $mysqlConfPath

cat >> $mysqlTargetConfFile <<EOF

# 以下是新增内容
# 标识不同的数据库服务器，而且唯一
server-id=1
# 启用二进制日志
log-bin=mysql-bin
binlog_format=row

EOF

docker cp $mysqlTargetConfFile $mysqlSourceConfFile

docker restart mysql

##============slave====================
docker run -d --name mysql_s --restart=always \
-p 3302:3306 \
-e MYSQL_ROOT_PASSWORD=testYnlcom \
--net mynet \
--ip 172.18.0.3 \
mysql:5.7

docker cp $mysql_sSourceConfFile $mysql_sConfPath

cat >> $mysql_sTargetConfFile <<EOF

# 以下是新增内容
server-id=2
log-bin=mysql-bin

EOF

docker cp $mysql_sTargetConfFile $mysql_sSourceConfFile

docker restart mysql_s


##master服务器执行=====================
# CREATE USER 'slave'@'%' IDENTIFIED BY 'slave123456';
# GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* to 'slave'@'%' ;
# show master status;

##slave 服务器执行，=====================
# 注意修改 master_host，master_log_file，master_log_pos=====================
#stop slave;
#change master to
#master_host='172.18.0.2',
#master_user='slave',
#master_password='slave123456',
#master_log_file='mysql-bin.000001',
#master_log_pos=619,
#master_port=3306;
#
#start slave;
#
#show slave status


##master服务器执行=====================
# CREATE DATABASE `canal-test` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
#CREATE TABLE `canal-test`.`product`  (
#  `id` bigint(20) NOT NULL AUTO_INCREMENT,
#  `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
#  `sub_title` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
#  `price` decimal(10, 2) NULL DEFAULT NULL,
#  `pic` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
#  PRIMARY KEY (`id`) USING BTREE
#) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;


#INSERT INTO product ( id, title, sub_title, price, pic ) VALUES ( 5, '小米8', ' 全面屏游戏智能手机 6GB+64GB', 1999.00, NULL );
#INSERT INTO product ( id, title, sub_title, price, pic ) VALUES ( 6, '小米9', '9 全面屏游戏智能手机 6GB+64GB', 2999.00, NULL );
#INSERT INTO product ( id, title, sub_title, price, pic ) VALUES ( 7, '小米10', '10 全面屏游戏智能手机 6GB+64GB', 3999.00, NULL );


# show variables like '%tx_isolation%';
# show variables like '%autocommit%';
